package com.wy.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.project.entity.*;
import com.wy.project.mapper.ProductInfoBizFlowInfoMapper;
import com.wy.project.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Apollo
 * @since 2021-01-16
 */
@Service
public class ProductInfoBizFlowInfoService extends ServiceImpl<ProductInfoBizFlowInfoMapper,ProductInfoBizFlowInfo> implements IProductInfoBizFlowInfoService {
    //产品基本信息
    @Resource
    IProductInfoService productInfoService;
    //产品业务流程信息
    @Resource
    IProductInfoBizFlowInfoService productInfoBizFlowInfoService;
    //历史产品业务流程信息
    @Resource
    IHisProductInfoBizFlowInfoService hisProductInfoBizFlowInfoService;
    //产品审核信息
    @Resource
    IProductApprovalInfoService productApprovalInfoService;
    //历史产品审核信息
    @Resource
    IHisProductApprovalInfoService hisProductApprovalInfoService;
    //产品消息
    @Resource
    IProductMsgService productMsgService;


    /**
     * 将产品业务流程信息
     * 存为草稿(创建)
     *
     * @param productInfo 要创建的产品基本信息,包含业务流程信息
     */
    @Override
    public void save4Draft(ProductInfo productInfo) {
        //保存业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo: productInfoBizFlowInfos){
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setIsDelete(2);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //修改产品基本信息状态
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_DRAFT);
        productInfoService.updateById(productInfo);
    }


    /**
     * 将产品业务流程信息
     * 提交审核(创建)
     *
     * @param productInfo 要创建的产品基本信息,包含业务流程信息
     */
    @Override
    public void save4Approval(ProductInfo productInfo) {
        //1:保存业务流程信息
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo: productInfoBizFlowInfos){
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setIsDelete(2);
            productInfoBizFlowInfo.setVersion(1);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //修改产品基本信息状态
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfoService.updateById(productInfo);
        //2:保存历史业务流程信息
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo: productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            BeanUtils.copyProperties(productInfoBizFlowInfo,hisProductInfoBizFlowInfo);
            hisProductInfoBizFlowInfo.setHisProductInfoCode(productInfo.getCode());
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
        }
        hisProductInfoBizFlowInfoService.saveBatch(hisProductInfoBizFlowInfos);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }



    /**
     * 将产品业务流程信息
     * 存为草稿(修改)
     *
     * @param productInfo 要修改的产品基本信息,包含业务流程信息
     */
    @Override
    public void edit4Draft(ProductInfo productInfo) {
        //修改业务流程信息
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        productInfoBizFlowInfoService.remove(productInfoBizFlowInfoQueryWrapper);
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo : productInfoBizFlowInfos){
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setIsDelete(2);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
            productInfo.setId((int)(Math.random()*10000));
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //把产品基本信息的状态修改成草稿
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_DRAFT);
        //修改数据库
        productInfoService.updateById(productInfo);
    }

    /**
     * 将产品业务流程信息
     * 提交审核(修改)
     *
     * @param productInfo 要修改的产品基本信息,包含业务流程信息
     */
    @Override
    public void edit4Approval(ProductInfo productInfo) {
        //1:修改业务流程信息
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        productInfoBizFlowInfoService.remove(productInfoBizFlowInfoQueryWrapper);
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo : productInfoBizFlowInfos){
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setIsDelete(2);
            productInfoBizFlowInfo.setVersion(1);
            productInfoBizFlowInfo.setId((int)(Math.random()*10000));
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //把产品基本信息的状态修改成草稿
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        //修改数据库
        productInfoService.updateById(productInfo);
        //2:保存历史业务流程信息
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo : productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            BeanUtils.copyProperties(productInfoBizFlowInfo,hisProductInfoBizFlowInfo);
            hisProductInfoBizFlowInfo.setHisProductInfoCode(productInfo.getCode());
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
        }
        hisProductInfoBizFlowInfoService.saveBatch(hisProductInfoBizFlowInfos);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }


    /**
     * 将产品业务流程信息
     * 审核通过
     *
     * @param productApprovalInfo 审核记录
     */
    @Override
    public void pass(ProductApprovalInfo productApprovalInfo) {
        //创建审核信息
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setType(2);//业务流程
        productApprovalInfo.setPass(1);
        productApprovalInfoService.save(productApprovalInfo);
        //修改状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productApprovalInfo.getProductInfoCode());
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        productInfoService.updateById(productInfo);
        //保存历史业务审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        Integer version = productInfo.getVersion();
        hisProductApprovalInfo.setVersion(version);
        hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(2);//审核通过
        String title=productInfo.getName()+"的业务流程信息审核通过";
        productMsg.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"审核通过";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
        Integer status = productInfo.getStatus();
        Integer pdInfoReqsStatus = productInfo.getPdInfoReqsStatus();
        if(ProductInfo.Status.STATUS_APPROVAL_PASS==status && ProductInfo.Status.STATUS_APPROVAL_PASS==pdInfoReqsStatus){
            ProductMsg productMsg1 = new ProductMsg();
            productMsg1.setSendTime(new Date());//发送时间
            productMsg1.setIsRead(2);//未读
            productMsg1.setHandle(2);//未处理
            productMsg1.setKind(2);//业务流程信息
            productMsg1.setType(2);//审核通过
            String title1=productInfo.getName()+"可以发布";
            productMsg1.setTitle(title1);
            String content1="您有一个产品全部审核通过了,可以进行发布";
            productMsg1.setContent(content1);
            productMsg1.setHisProductInfoCode(productInfo.getCode());
        }
    }

    /**
     * 将产品业务流程信息
     * 审核不通过
     *
     * @param productApprovalInfo 审核记录
     */
    @Override
    public void notPass(ProductApprovalInfo productApprovalInfo) {
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setType(2);//业务流程
        productApprovalInfo.setPass(2);
        productApprovalInfoService.save(productApprovalInfo);
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productApprovalInfo.getProductInfoCode());
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_APPROVAL_FAIL);
        productInfoService.updateById(productInfo);
        //保存历史业务审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        hisProductApprovalInfo.setVersion(productInfo.getVersion());
        hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(3);//审核失败
        String title=productInfo.getName()+"的业务流程信息审核失败";
        productMsg.setTitle(title);
        String content="您有一个产品业务流程审核失败";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }


    /**
     * 将产品业务流程信息
     * 再次审核
     * 审核失败之后,进行信息的修改,之后提交审核
     *
     * @param productInfo 要修改的产品基本信息,包含业务流程信息
     */
    @Override
    public void revise(ProductInfo productInfo) {
        //修改业务流程信息
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,productInfo.getCode());
        productInfoBizFlowInfoService.remove(productInfoBizFlowInfoQueryWrapper);
        List<ProductInfoBizFlowInfo> productInfoBizFlowInfos = productInfo.getProductInfoBizFlowInfos();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo : productInfoBizFlowInfos){
            productInfoBizFlowInfo.setCreateTime(new Date());
            productInfoBizFlowInfo.setUpdateTime(new Date());
            productInfoBizFlowInfo.setIsDelete(2);
            productInfoBizFlowInfo.setVersion(productInfo.getVersion()+1);
            productInfoBizFlowInfo.setProductInfoCode(productInfo.getCode());
        }
        productInfoBizFlowInfoService.saveBatch(productInfoBizFlowInfos);
        //修改业务流程状态
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        productInfoService.updateById(productInfo);
        //保存历史版本
        ArrayList<HisProductInfoBizFlowInfo> hisProductInfoBizFlowInfos = new ArrayList<>();
        for(ProductInfoBizFlowInfo productInfoBizFlowInfo : productInfoBizFlowInfos){
            HisProductInfoBizFlowInfo hisProductInfoBizFlowInfo = new HisProductInfoBizFlowInfo();
            hisProductInfoBizFlowInfo.setCreateTime(productInfoBizFlowInfo.getCreateTime());
            hisProductInfoBizFlowInfo.setUpdateTime(productInfoBizFlowInfo.getUpdateTime());
            hisProductInfoBizFlowInfo.setIsDelete(2);
            hisProductInfoBizFlowInfo.setHisProductInfoCode(productInfo.getCode());
            hisProductInfoBizFlowInfo.setCode(productInfoBizFlowInfo.getCode());
            hisProductInfoBizFlowInfo.setDes(productInfoBizFlowInfo.getDes());
            hisProductInfoBizFlowInfo.setName(productInfoBizFlowInfo.getName());
            hisProductInfoBizFlowInfo.setUrl(productInfoBizFlowInfo.getUrl());
            hisProductInfoBizFlowInfo.setVersion(productInfoBizFlowInfo.getVersion()+1);
            hisProductInfoBizFlowInfos.add(hisProductInfoBizFlowInfo);
        }
        hisProductInfoBizFlowInfoService.saveBatch(hisProductInfoBizFlowInfos);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        productMsg.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);

    }

    /**
     * 将产品业务流程信息
     * 作废
     *
     * @param code 要作废的信息标识
     */
    @Override
    public void abort(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_ABORT);
        productInfoService.updateById(productInfo);
        //3:发送消息
        ProductMsg productMsg = new ProductMsg();
        productMsg.setSendTime(new Date());//发送时间
        productMsg.setIsRead(2);//未读
        productMsg.setHandle(2);//未处理
        productMsg.setKind(2);//业务流程信息
        productMsg.setType(4);//作废
        String title=productInfo.getName()+"的业务流程信息已作废";
        productMsg.setTitle(title);
        String content="有一个产品业务流程"+productInfo.getName()+"已经作废";
        productMsg.setContent(content);
        productMsg.setHisProductInfoCode(productInfo.getCode());
        productMsgService.save(productMsg);
    }

    /**
     * 产品业务流程信息
     * 查看详情
     *
     * @param code 要查看的标识
     * @return 详情信息
     */
    @Override
    public List<ProductInfoBizFlowInfo> findProductInfoBiz(String code) {
        //根据标识查询产品基本信息
        QueryWrapper<ProductInfoBizFlowInfo> productInfoBizFlowInfoQueryWrapper = new QueryWrapper<>();
        productInfoBizFlowInfoQueryWrapper.lambda().eq(ProductInfoBizFlowInfo::getProductInfoCode,code);
        List<ProductInfoBizFlowInfo> list = productInfoBizFlowInfoService.list(productInfoBizFlowInfoQueryWrapper);
        return list;
    }
}
